package cn.net.yato.picture.controller.backstage;

import cn.net.yato.picture.entity.Permission;
import cn.net.yato.picture.entity.Role;
import cn.net.yato.picture.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName HomeController
 * @Description TODO
 * @Author yato
 * @Date 2020/2/19 11:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/backstage")
public class HomeController {

    @GetMapping("/home.html")
    public ModelAndView home(ModelAndView modelAndView){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Set<Permission> set = new HashSet<>(user.getPermissions());
        /*合并角色权限和用户权限*/
        for (Role role : user.getRoles()) {
            set.addAll(role.getPermissions());
        }
        List<Permission> list = new ArrayList<>(set);
        modelAndView.addObject("allPermissions", list);
        modelAndView.setViewName("backstage/home");
        return modelAndView;
    }

}
